package com.browsesoft.user;

import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLMenuModel;

/**
 * 功能菜单
 * 
 * @author Browsesoft
 * @version 1.0
 */
public class FunctionMenuModel implements HTMLMenuModel {
	/**
	 * 功能菜单所对应的用户
	 */
	private User user = null;

	/**
	 * 初始化model
	 * 
	 * @throws Exception
	 */
	public void init(HttpServletRequest request) throws Exception {
	}

	/**
	 * 处理用户请求
	 * 
	 * @param request
	 *            客户端请求
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) {
		// 装载用户的所有功能
		user = (User) request.getSession(false).getAttribute("loginUser");
		user.loadFunctions();
	}

	/**
	 * 取得根结点
	 * 
	 * @return 根结点
	 */
	public LinkedList getRootNodes() throws Exception {
		return user.getRootFunctions();
	}

	/**
	 * 根据结点返回子结点
	 * 
	 * @return 子结点
	 */
	public LinkedList getChildNodes(Object node) throws Exception {
		return user.getChildFunctions((Function) node);
	}

	/**
	 * 根据结点返回结点的名称
	 * 
	 * @return 结点的名称
	 */
	public String getName(Object node) throws Exception {
		return (String) ((Function) node).getAttributes().get("name");
	}

	/**
	 * 根据结点返回结点的链接
	 * 
	 * @return 结点的链接
	 */
	public String getLink(Object node) throws Exception {
		return (String) ((Function) node).getAttributes().get("link");
	}

	/**
	 * 设置模型所对应的组件
	 * 
	 * @param component
	 *            对应的组件
	 */
	public void setComponent(HTMLBasicComponent component) {
	}

	/**
	 * 模型更新页面元素
	 * 
	 * @throws Exception
	 */
	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}
}